package com.jeeplus.mob.service;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.mob.common.StringHelper;
import com.jeeplus.mob.entity.mobpojo.BuildingcardrecordInfo;
import com.jeeplus.mob.entity.mobpojo.CardrecordInfo;
import com.jeeplus.mob.mapper.BuildingcardrecordInfoMapper;
import com.jeeplus.mob.mapper.CardrecordInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BuildingcardrecordService {

    @Autowired
    private CardrecordInfoMapper cardrecordInfoMapper;

    @Autowired
    private BuildingcardrecordInfoMapper buildingcardrecordInfoMapper;

    /**
     * 楼宇打卡
     * @param buildingcardrecordInfo
     * @return
     */
    public AjaxJson punchBuilding(BuildingcardrecordInfo buildingcardrecordInfo) {
        CardrecordInfo cardrecordInfo = new CardrecordInfo();
        if (StringHelper.IsEmptyOrNull(buildingcardrecordInfo)) {
            return AjaxJson.success("无参数请求，请传参！");
        }
        if (StringHelper.IsEmptyOrNull(buildingcardrecordInfo.getPunchNumber())) {
            return AjaxJson.success("在途打卡未打卡，请打卡！");
        }
        int rows = buildingcardrecordInfoMapper.insertBuildingCardrecord(buildingcardrecordInfo);
        buildingcardrecordInfo.getId();
        if (rows > 0) {
            cardrecordInfo.setBuildingcardrecordId(buildingcardrecordInfo.getId().toString());
            cardrecordInfo.setPunchNumber(buildingcardrecordInfo.getPunchNumber());
            cardrecordInfo.setUserId(buildingcardrecordInfo.getUserId());
            cardrecordInfo.setPunchDate(buildingcardrecordInfo.getPunchDate());
            cardrecordInfo.setPunchPicture(buildingcardrecordInfo.getPunchPicture());
            cardrecordInfo.setBuildingId(buildingcardrecordInfo.getBuildingId());
            cardrecordInfo.setPunchAddress(buildingcardrecordInfo.getPunchAddress());
            cardrecordInfo.setPunchType("2");
            cardrecordInfo.setCreateBy(buildingcardrecordInfo.getUserId());
            cardrecordInfo.setCreateDate(buildingcardrecordInfo.getPunchDate());
            cardrecordInfo.setUpdateBy(buildingcardrecordInfo.getUserId());
            cardrecordInfo.setUpdateDate(buildingcardrecordInfo.getPunchDate());
            cardrecordInfo.setAuditStatus("0");
            cardrecordInfo.setDeleteFlag("0");
            int row = cardrecordInfoMapper.insertSelective(cardrecordInfo);
            if (row > 0) {
                return AjaxJson.success("打卡成功!");  // 楼宇打卡成功后，前端要将uuid从浏览器移除
            }

        }
        return AjaxJson.success("打卡失败，请重试！");
    }
}
